import { _decorator,sp,geometry,Label, assetManager,Tween,Camera, Node,Color,math, Sprite,SystemEvent, LabelComponent, SkinnedMeshRenderer,Prefab,v3, Vec3,SkeletalAnimation, ProgressBar, tween } from 'cc';
const { Skeleton } = sp; 
import BaseView from '../../Script/game/BaseView';
// import { luckyBox } from './luckyBox';
import GD from '../../Script/config/GD';
import GDef from '../../Script/config/GDef';
import Tool from '../../Script/tool/Tool'
const { bits } = math;
const { ccclass, property } = _decorator;

@ccclass('preProgress')
export class preProgress extends BaseView {
    @property({
        type: Label
    })
    labelPercent: Label = null;
    @property({
        type: [ProgressBar]
    })
    arrLevelProgress: Array<ProgressBar> = [];
    @property({
        type: Node
    })
    nodeManghe: Node = null;
    @property({
        type: sp.Skeleton
    })
    ske: sp.Skeleton = null;
    
    onLoad() {
        this.on(GDef.wdEvent.levelStart,this.levelStart);
        this.levelStart();
    }
    addProgress(parent: Node){
        var self = this;
        //進度條
        let index = (GD.getLevel() - 1) % 4;
        this.labelPercent.string = Math.floor(index / 4 * 100) + "";
        for(let i = 0; i< this.arrLevelProgress.length; i++)
        {
            this.arrLevelProgress[i].progress = i < index?1:0;
        }

        let src = parseInt(this.labelPercent.string);
        let add = 25;
        const tweenTargetVec32 = new Vec3();
        this.tweenProgress = new Tween(tweenTargetVec32)
            .by(0.3, new Vec3(1, 1, 1), {
                'onUpdate': (target: object | undefined, ratio: number | undefined) => {
                    if(self && self.labelPercent && self.arrLevelProgress)
                    {
                        console.log("progress : " + src + ratio * add);
                        self.labelPercent.string = parseInt(src + ratio * add) + "";
                        self.arrLevelProgress[index].progress = ratio;
                        console.log("index : " + index);
                    }
                }
            }).call(()=>{
                self.emit(GDef.wdEvent.playEffect,"Button");
                GD.addLevel();
                if(index % 4 == 3)
                {
                    if(self)
                    {
                        console.log("盲盒");
                        //盲盒
                        console.log("盲盒 createNode");
                        Tool.createNode(GDef.pack2,GDef.preManghe).then((node) =>{
                            console.log("盲盒 createNode parent");
                            node.parent = parent;
                            GDef.nodeManghe = node;
                            console.log("盲盒 createNode startPlay");
                            GDef.nodeManghe.getComponent("preManghe").startPlay();
                            console.log("盲盒 createNode startPlay success");
                        }).catch(err => {
                            console.error(err);
                            return null;
                        });
                    }
                }
            });
        this.tweenProgress.start();
    }
    levelStart(){
        var self = this;
        let index = (GD.getLevel() - 1) % 4;
        this.labelPercent.string = Math.floor(index / 4 * 100) + "";
        for(let i = 0; i< this.arrLevelProgress.length; i++)
        {
            this.arrLevelProgress[i].progress = i < index?1:0;
        }
    }
}   
